package o;

import android.content.Context;
import android.net.Uri;
import android.provider.MediaStore.Images.Media;
import android.provider.MediaStore.Video;
import com.huawei.appgallery.common.media.api.OriginalMediaBean;
import com.huawei.appgallery.foundation.storage.db.RecordBean;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public final class bln {
    private static final String[] ˎ = new String[]{"_id", "_size", "_data", "bucket_display_name", "width", "height", "mime_type", "orientation"};
    private static bln ˏ;
    private bls ʻ;
    private Map<String, List<OriginalMediaBean>> ʼ = new HashMap();
    private String[] ʽ;
    private List<OriginalMediaBean> ˊ = new ArrayList();
    private List<blg> ˋ = new ArrayList();
    private bll ॱ = bll.ॱ();
    private String[] ॱॱ;
    private String ᐝ;

    private bln() {
    }

    public static synchronized bln ˊ() {
        bln o_bln;
        synchronized (bln.class) {
            if (ˏ == null) {
                ˏ = new bln();
            }
            o_bln = ˏ;
        }
        return o_bln;
    }

    public synchronized void ॱ(Context context, String str, String[] strArr, String[] strArr2) {
        if (!this.ˊ.isEmpty()) {
            this.ˊ.clear();
        }
        if (strArr == null) {
            this.ʽ = new String[0];
        } else {
            this.ʽ = (String[]) strArr.clone();
        }
        if (strArr2 == null) {
            strArr2 = new String[]{"image/jpeg", "image/jpg", "image/png"};
        }
        this.ᐝ = str;
        this.ॱॱ = strArr2;
        this.ˊ = ˏ(context, this.ᐝ, this.ॱॱ);
    }

    public void ˋ() {
        ʻ();
        ʼ();
    }

    public synchronized Map<String, List<OriginalMediaBean>> ˏ() {
        return new HashMap(this.ʼ);
    }

    public synchronized List<blg> ॱ() {
        return new ArrayList(this.ˋ);
    }

    public synchronized List<OriginalMediaBean> ˎ() {
        return new ArrayList(this.ˊ);
    }

    private synchronized void ʼ() {
        if (!(this.ˋ == null || !this.ˋ.isEmpty() || this.ʼ == null || this.ʼ.isEmpty())) {
            OriginalMediaBean originalMediaBean = (OriginalMediaBean) this.ˊ.get(0);
            blg o_blg = new blg();
            o_blg.ˋ(originalMediaBean.ʽ());
            o_blg.ˎ("all_medias");
            o_blg.ˏ(this.ˊ.size());
            o_blg.ˊ(originalMediaBean.ᐝ());
            o_blg.ॱ(originalMediaBean.ˊ());
            this.ˋ.add(o_blg);
            for (Entry key : this.ʼ.entrySet()) {
                List list = (List) this.ʼ.get(key.getKey());
                if (!(list == null || list.isEmpty())) {
                    OriginalMediaBean originalMediaBean2 = (OriginalMediaBean) list.get(0);
                    blg o_blg2 = new blg();
                    o_blg2.ˋ(originalMediaBean2.ʽ());
                    o_blg2.ˎ(originalMediaBean2.ॱॱ());
                    o_blg2.ˏ(list.size());
                    o_blg2.ˊ(originalMediaBean2.ᐝ());
                    o_blg2.ॱ(originalMediaBean.ˊ());
                    this.ˋ.add(o_blg2);
                }
            }
        }
    }

    public synchronized void ʽ() {
        if (this.ˊ != null) {
            this.ˊ.clear();
        }
        if (this.ˋ != null) {
            this.ˋ.clear();
        }
        if (this.ʼ != null) {
            this.ʼ.clear();
        }
    }

    private synchronized void ʻ() {
        if (!(this.ʼ == null || !this.ʼ.isEmpty() || this.ˊ == null || this.ˊ.isEmpty())) {
            this.ʼ = ॱ(this.ˊ, OriginalMediaBean.class, "bucket_display_name_");
        }
    }

    private <T extends RecordBean> Map<String, List<T>> ॱ(List<T> list, Class<T> cls, String str) {
        Map<String, List<T>> hashMap = new HashMap();
        if (!(list == null || list.isEmpty())) {
            try {
                for (T t : list) {
                    if (t != null) {
                        List list2;
                        Field declaredField = t.getClass().getDeclaredField(str);
                        declaredField.setAccessible(true);
                        String str2 = (String) declaredField.get(t);
                        if (hashMap.containsKey(str2)) {
                            list2 = (List) hashMap.get(str2);
                        } else {
                            list2 = new ArrayList();
                        }
                        list2.add(t);
                        hashMap.put(str2, list2);
                    }
                }
            } catch (Throwable e) {
                blf.ˋ.ˊ("ThumbnailManager", "getField fail", e);
            } catch (Throwable e2) {
                blf.ˋ.ˊ("ThumbnailManager", "getField fail", e2);
            } catch (Throwable e22) {
                blf.ˋ.ˊ("ThumbnailManager", "getField fail", e22);
            }
        }
        return hashMap;
    }

    private List<OriginalMediaBean> ˏ(Context context, String str, String[] strArr) {
        if ("video".equals(str)) {
            return ˏ(context);
        }
        return ॱ(context, strArr);
    }

    private List<OriginalMediaBean> ॱ(Context context, String[] strArr) {
        int i = 0;
        Uri uri = Media.EXTERNAL_CONTENT_URI;
        StringBuilder stringBuilder = new StringBuilder();
        Object obj = new String[strArr.length];
        System.arraycopy(strArr, 0, obj, 0, strArr.length);
        while (i < strArr.length) {
            stringBuilder.append("mime_type").append("=?");
            if (i + 1 != strArr.length) {
                stringBuilder.append(" or ");
            }
            i++;
        }
        bll o_bll = this.ॱ;
        String[] strArr2 = ˎ;
        String stringBuilder2 = stringBuilder.toString();
        List ˋ = o_bll.ˋ(context, uri, strArr2, stringBuilder2, obj, "date_added desc", "image");
        ˏ(ˋ);
        return ˋ;
    }

    private List<OriginalMediaBean> ˏ(Context context) {
        String[] strArr = new String[]{"video/mp4", "Screenshots"};
        List ˋ = this.ॱ.ˋ(context, Video.Media.EXTERNAL_CONTENT_URI, null, "mime_type=? and bucket_display_name=?", strArr, "date_added desc", "video");
        if (this.ʻ != null) {
            List<OriginalMediaBean> ˋ2 = this.ʻ.ˋ(ˋ);
            if (!cgs.ˋ(ˋ2)) {
                return ˋ2;
            }
        }
        return new ArrayList();
    }

    private void ˏ(List<OriginalMediaBean> list) {
        if (list != null && !cgp.ˏ(this.ʽ)) {
            for (int size = list.size() - 1; size >= 0; size--) {
                if (!ˎ(list.get(size) == null ? null : ((OriginalMediaBean) list.get(size)).ʽ(), this.ʽ)) {
                    list.remove(size);
                }
            }
        }
    }

    private boolean ˎ(String str, String[] strArr) {
        if (str == null) {
            return false;
        }
        if (cgp.ˏ(strArr)) {
            return true;
        }
        for (String endsWith : strArr) {
            if (str.endsWith(endsWith)) {
                return true;
            }
        }
        return false;
    }

    public synchronized ArrayList<OriginalMediaBean> ˏ(Context context, String str, String str2, String[] strArr, String[] strArr2) {
        ArrayList<OriginalMediaBean> arrayList;
        if (cgy.ˎ(str)) {
            arrayList = null;
        } else {
            arrayList = new ArrayList();
            if (this.ʼ == null || this.ʼ.size() == 0) {
                ॱ(context, str2, strArr, strArr2);
                ʻ();
            }
            if (this.ʼ != null) {
                if (str.equals("all_medias")) {
                    arrayList = (ArrayList) ˎ();
                } else {
                    arrayList = (ArrayList) this.ʼ.get(str);
                }
            }
        }
        return arrayList;
    }
}
